function sensornet_server_file_processing

%% GLOBAL configuration
global INPUT_DIR;
global AMR_DIR;
global WAV_DIR;
global DECODED_DIR;
global DB_DIR;

global AMR2WAV_TOOL;

AMR2WAV_TOOL = 'amr2wav.bat'
root_dir = 'WEB';

INPUT_DIR = [root_dir filesep 'INPUT'];
AMR_DIR = [root_dir filesep 'DATA' filesep 'AMR'];
WAV_DIR = [root_dir filesep 'DATA' filesep 'WAV'];
DECODED_DIR = [root_dir filesep 'DATA' filesep 'DECODED'];
DB_DIR = [root_dir filesep 'DATA' filesep 'DB'];

%% INTIALIZATION

if (isdir(root_dir))
    %disp('Exists')
else 
    disp('Creating default directory structure...')
    mkdir(root_dir);
    mkdir(INPUT_DIR);
    mkdir(AMR_DIR);
    mkdir(WAV_DIR);
    mkdir(DECODED_DIR);
    mkdir(DB_DIR);
end


%% process input files

files_dir = INPUT_DIR;
disp(['Processing input directory:', files_dir]);
files = dir([files_dir filesep '*.amr']);
nfiles = size(files,1);
for j=1:nfiles
	filename = files(j).name;
    file_fullname = [files_dir filesep filename];
    % parse metadata
    metadata = parse_metadata(filename);
    disp(['Processing...:', file_fullname]);
    metadata.amr_filesize = 0;
    % AMR conversion to WAV
    wav_fullname = amr2wav(file_fullname, WAV_DIR, metadata.id);
    %%%%movefile(file_fullname, AMR_DIR);
    % FFT analysis
    decoded_fullname = decode_wav(wav_fullname, DECODED_DIR, metadata.id);
    % Add info to database
    add_to_DB(decoded_fullname, DB_DIR, metadata);
end

end

function metadata = parse_metadata(filename)
    metadata.filename = filename;
    metadata.id = filename;
    metadata.date = 'yyyymmdd';
    metadata.hour = 'hhmmss';
    metadata.timestamp = 0;
    metadata.sensor = 'sensor_id';
end

function wav_fullname = amr2wav(file_fullname, output_dir, id)
    global AMR2WAV_TOOL;
	
    filename = strcat(id, '.wav');
    wav_fullname = fullfile(output_dir, filename);

    system( [AMR2WAV_TOOL ' ' file_fullname ' ' wav_fullname]);

	disp(['  amr2wav done: ' file_fullname]);
end

function decoded_fullname = decode_wav(file_fullname, output_dir, id)
    freq = obtain_main_frequency(file_fullname);
    data = [freq 0 0 0];
    filename = strcat(id, '.txt');
    decoded_fullname = fullfile(output_dir, filename);
%save(decoded_fullname, freq -ascii -tabs
    fid = fopen(decoded_fullname, 'w+');
    fprintf(fid, '%s\n', ['filename :' id]);
    fprintf(fid, '%6.2f\t%6.2f\t%6.2f\t%12.8f\n', data);
    fclose(fid);
	disp(['  decode_wav done: ' file_fullname]);
end

function add_to_DB(decoded_fullname, db_dir, metadata)
    disp('TODO: add_to_DB()');
end


function frequency = obtain_main_frequency(wav_fullname)

%% Read a wav file
%Fs: Sampling frequency, nbits:number of bits per sample
[y, Fs, nbits] = wavread(wav_fullname);

T = 1/Fs;            % Sample time
L = length(y);      % Length of signal
%t = (0:L-1)*T;      % Time vector

%% Select interval

interval_duration = 0.5; %seconds
number_of_samples = round(interval_duration/T);
start_sample = round(L/2);
yy = y(start_sample: start_sample+number_of_samples-1);
LL = length(yy);

%% FFT 
NFFT = 512; %NFFT = 2^nextpow2(L);
Y = fft(yy,NFFT)/LL;
f = Fs/2*linspace(0,1,NFFT/2);

%% Main peak
[val,pos]=max(abs(Y(1:NFFT/2)));
peak_freq = round(f(pos));
frequency = peak_freq;
end 
